package link;

import array.ListNode;
//todo
public class SplitLinkedListInParts725 {
    public ListNode[] splitListToParts(ListNode head, int k) {
        int l = 0;
        ListNode temp = head;
        while (temp != null) {
            temp = temp.next;
            l++;
        }
        int length = l / k;
        int res = l % k;
        ListNode[] result = new ListNode[k];
        temp = head;
        for (int i = 0; i < result.length; i++) {
            if (temp != null) {
                int j = length;
                ListNode node = temp;
                while (j-- > 0) {
                    temp = temp.next;
                }
                if (res-- > 0) {
                    temp = temp.next;
                }
                if (temp != null) {
                    ListNode t = temp.next;
                    temp.next = null;
                    temp = t;
                }
                result[i] = node;
            }
        }
        return result;
    }
}
